﻿Imports System.Reflection

Public Class frmSale
    Dim dt As New DataTable
    Public itemid = 0
    Dim total = 0
    Public Sub New()
        InitializeComponent()

        'UI.SetPlaceHolder(txtcus, "អតិថិជន")
        'UI.SetPlaceHolder(txtemp, "បុគ្គលិក")
        'UI.SetPlaceHolder(txtpro, "Barcode or Item name")

        'MycustomeDate.datimepikerkhmer(TextBox1, DateTimePicker1)
        'myDataset.dataset()
        'Getdata()
        UI.FormatDataGrid(dgvlist)
        loadinNo()
        loadCorp()
        loadpayment()
        loademp()
        GroupPay.Visible = False
    End Sub


    Private Sub dgvlist_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvlist.RowPrePaint
        If e.RowIndex >= 0 Then
            Me.dgvlist.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
        End If
    End Sub


    Private Sub checkpay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkpay.CheckedChanged
        If checkpay.Checked = False Then
            GroupPay.Visible = True
        Else
            GroupPay.Visible = False
        End If
    End Sub

    Private Sub loadCorp()
        cbocorporate.DisplayMember = "corporate_name"
        cbocorporate.ValueMember = "corporate_id"
        cbocorporate.DataSource = Cmd.ExecuteDataTable("select corporate_id, corporate_name from tbl_corporate where active=1")
    End Sub
    Private Sub loadinNo()
        Dim no = Cmd.ExecuteScalar("select MAX(invoice_code) from tbl_sale") + 1
        lblinNo.Text = no.ToString.PadLeft(8, "0"c)
    End Sub
    Private Sub loadpayment()
        Dim dtp = Cmd.ExecuteDataTable("select * from tbl_exchange_rate_detail where exchange_rate_Detail_id=4")
        cboPayMethod.DisplayMember = "currency_name"
        cboPayMethod.ValueMember = "exchange_rate_detail_id"
        cboPayMethod.DataSource = dtp
    End Sub
    Private Sub loademp()
        cboemp.DisplayMember = "employee_name"
        cboemp.ValueMember = "employee_id"
        cboemp.DataSource = Cmd.ExecuteDataTable("select employee_id, employee_name from tbl_employee where active=1")
    End Sub

    Private Sub cbocorporate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbocorporate.SelectedIndexChanged
        txtcornumber.Text = Cmd.ExecuteScalar("select phone from tbl_corporate where corporate_id='" & cbocorporate.SelectedValue & "'")
    End Sub

    Private Sub cboemp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboemp.SelectedIndexChanged
        txtempnumber.Text = Cmd.ExecuteScalar("select phone from tbl_employee where employee_id='" & cboemp.SelectedValue & "'")
    End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Dim frm As New frmViewProSale
        frm.ShowDialog()
    End Sub

    Private Sub txtcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcode.TextChanged
        If loadItem() Then
            txtqty.Focus()
            txtqty.SelectAll()
        End If
    End Sub

    Private Function loadItem() As Boolean
        Dim dt As New DataTable
        Try
            Cmd.Parameters("@search") = txtcode.Text
            dt = Cmd.ExecuteDataTable("select i.item_id,sell_out_riel ,i.item_name,i.photo from tbl_stock s inner join tbl_item i on i.item_id=s.item_id  where i.item_code=@search and i.active= true")
            itemid = dt.Rows(0).Item("item_id")
            txtitemname.Text = dt.Rows(0).Item("item_name")
            txtprice.Text = dt.Rows(0).Item("sell_out_riel")
            Dim bytes As Byte() = DirectCast(dt.Rows(0).Item("photo"), Byte())
            Dim m As New System.IO.MemoryStream(bytes)
            picitem.Image = New System.Drawing.Bitmap(m)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Sub txtqty_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtqty.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
        End If
    End Sub
    
    Private Sub addRow()
        If itemid <> 0 Then
            Dim i = Me.dgvlist.Rows.Add
            Dim row = Me.dgvlist.Rows(i)
            total = (Val(txtqty.Text) * Val(txtprice.Text))
            total = CInt(total - ((total * Val(txtdiscount.Value)) / 100))
            row.Cells("item_id").Value = itemid
            row.Cells("item_name").Value = txtitemname.Text
            row.Cells("item_price").Value = txtprice.Text
            row.Cells("item_qty").Value = txtqty.Text
            row.Cells("amount").Value = total
            row.Cells("item_discount").Value = txtdiscount.Value
            row.Cells("subamount").Value = Val(txtqty.Text) * Val(txtprice.Text)
            lblsubtotal.Text = CInt(Val(lblsubtotal.Text) + total)

            lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(nupvat.Value) / 100)))

            txtamount.Text = lblTotal.Text
            txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
            clearItem()
        End If

    End Sub

    Private Sub clearItem()
        txtcode.Text = ""
        txtqty.Text = 1
        txtitemname.Text = "N/A"
        txtdiscount.Value = 0
        txtprice.Text = 0
        picitem.Image = Nothing
    End Sub

    Private Sub clearAll()
        clearItem()
        cbocorporate.SelectedIndex = 0
        lblinNo.Text = (CInt(lblinNo.Text) + 1).ToString.PadLeft(8, "0"c)
        txtamount.Text = 0
        txtowe.Text = 0
        lblsubtotal.Text = 0
        lblTotal.Text = 0
        nupvat.Value = 0
        Nupdes.Value = 0
        txtdeposit.Text = 0
        txtshipping.Text = 0
        dgvlist.Rows.Clear()
        cboemp.SelectedIndex = 0
    End Sub
    
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim status = IIf(txtowe.Text = 0, 1, 0)
        ClassSale.addSale(lblinNo.Text, cbocorporate.SelectedValue, cboemp.SelectedValue, txtshipping.Text, lblsubtotal.Text, Nupdes.Value, nupvat.Value, lblTotal.Text, dgvlist, status, txtamount.Text, txtdeposit.Text)
        clearAll()
    End Sub

    Private Sub txtamount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtamount.TextChanged
        txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
    End Sub
End Class